<?php
namespace App\Http\Controllers\Business;

use App\Models\Valve;

class ValveModule {
    public static function valveList($page=1, $length=50) {
        $total = Valve::count();
        $lastPage = ceil($total / $length);
        $skip = ($page -1) * $length;
        $valveList = Valve::select('id', 'name', 'number', 'area', 'latitude', 'longitude', 'caliber', 'father_node')
            ->skip($skip)->take($length)->get();
        $data = [
            'page' => $page,
            'length' => $length,
            'total' => $total,
            'last_page' => $lastPage,
            'lists' => $valveList,
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    public static function getValve($id) {
        if(!$id) {
            return ['status'=>'failed', 'message'=>'参数不对'];
        }
        $valve = Valve::where('id', $id)->first();
        if(!$valve) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        return ['status'=>'success', 'data'=>$valve];
    }
    public static function addValve($number, $name, $caliber, $area, $longitude,$latitude, $fatherNode) {
        if(!$number || !$name) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $valve = Valve::create([
            'name' => $name,
            'number' => $number,
            'caliber' => $caliber,
            'area' => $area,
            'latitude' => $latitude,
            'longitude' => $longitude,
            'father_node' => $fatherNode,
        ]);
        return ['status'=>'success', 'message'=>'新增成功'];
    }
    public static function editValve($id, $name, $caliber, $area, $latitude, $longitude, $fatherNode) {
        if(!$id) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $valve = Valve::where('id', $id)->first();
        if(!$valve) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $valve->name = $name;
        $valve->caliber = $caliber;
        $valve->area = $area;
        $valve->latitude = $latitude;
        $valve->longitude = $longitude;
        $valve->father_node = $fatherNode;
        $valve->save();
        return ['status'=>'success', 'message'=>'修改成功'];
    }
    //肿瘤医院树状结构
    public static function cancerHospitalTree() {
        $fatherNode = 'ch00';
        $basisInfo = Valve::where('number', $fatherNode)->select('id', 'number','name', 'area', 'father_node')->first();
        $firstClassNodeList = Valve::where('father_node', $fatherNode)->select('id', 'number','name', 'area', 'father_node')->get();
        foreach ($firstClassNodeList as $v =>$k) {
            $secondClassNodeList = [];
            $firstClassNodeList[$v]['second_class_node_list'] = $secondClassNodeList;
        }
        $data = [
            'base_info' => $basisInfo,
            'child_list' => $firstClassNodeList
        ];
        return ['status'=>'success', 'data'=>$data];
    }
}
?>
